package org.richin.web.struts.util;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.security.Principal;
import java.util.Enumeration;
import java.util.Locale;
import java.util.Map;
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletInputStream;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

/**
 *  Facade to a HttpServletRequest. Struts has certin expectations
 *  about requests that it uses to made decisions. OpenCms has different
 *  expectations that causes problems when passing an OpenCms request
 *  to Struts. This class reestablishes the expectations Struts has. 
 * 
 * @author Kenneth Lewelling
 */

public class StrutsHttpServletRequest implements HttpServletRequest{

	private HttpServletRequest request = null;

	private String servletPath = null;

	private String  servletMapping = null;

	/**
	 * Constructor takes a HttpServletRequest and saves it. All further 
	 * requests to CmsStrutsHttpServletRequest are forwarded to the
	 * saved request and its result is returned, with a few exceptions 
	 * needed to fullfill the Struts expectations. 
	 */
	public StrutsHttpServletRequest(HttpServletRequest req){
		
		this.request = req;
	}

	/**
	 * @see javax.servlet.http.HttpServletRequest#getAuthType()
	 */
	public String getAuthType() {
		return this.request.getAuthType();
	}

	/**
	 * @see javax.servlet.http.HttpServletRequest#getContextPath()
	 */
	public String getContextPath() {
		return this.request.getContextPath();
	}

	/**
	 * @see javax.servlet.http.HttpServletRequest#getCookies()
	 */
	public Cookie[] getCookies() {
		return this.request.getCookies();
	}

	/**
	 * @see javax.servlet.http.HttpServletRequest#getDateHeader(String)
	 */
	public long getDateHeader(String arg0) {
		return this.request.getDateHeader(arg0);
	}

	/**
	 * @see javax.servlet.http.HttpServletRequest#getHeader(String)
	 */
	public String getHeader(String arg0) {
		return this.request.getHeader(arg0);
	}

	/**
	 * @see javax.servlet.http.HttpServletRequest#getHeaderNames()
	 */
	public Enumeration getHeaderNames() {
		return this.request.getHeaderNames();
	}

	/**
	 * @see javax.servlet.http.HttpServletRequest#getHeaders(String)
	 */
	public Enumeration getHeaders(String arg0) {
		return this.request.getHeaders(arg0);
	}

	/**
	 * @see javax.servlet.http.HttpServletRequest#getIntHeader(String)
	 */
	public int getIntHeader(String arg0) {
		return this.request.getIntHeader(arg0);
	}

	/**
	 * @see javax.servlet.http.HttpServletRequest#getMethod()
	 */
	public String getMethod() {
		return this.request.getMethod();
	}


	/**
	 * Accepts a modified path info, this will usualy be the 
	 * action name
	 * @param pathInfo String
	 */
	public void setPathInfo(String pathInfo){
		this.servletMapping = pathInfo;
	}

	/**
	 *  Returns a modifed Path info relative to the Struts module
	 *  @return String
	 */
	public String getPathInfo() {
		
		if(this.servletMapping != null ){
			return this.servletMapping;
		}
		
		return  this.request.getPathInfo();
	}

	/**
	 * @see javax.servlet.http.HttpServletRequest#getPathTranslated()
	 */
	public String getPathTranslated() {
		return this.request.getPathTranslated();
	}

	/**
	 * @see javax.servlet.http.HttpServletRequest#getQueryString()
	 */
	public String getQueryString() {
		return this.request.getQueryString();
	}

	/**
	 * @see javax.servlet.http.HttpServletRequest#getRemoteUser()
	 */
	public String getRemoteUser() {
		return this.request.getRemoteUser();
	}

	/**
	 * @see javax.servlet.http.HttpServletRequest#getRequestedSessionId()
	 */
	public String getRequestedSessionId() {
		return this.request.getRequestedSessionId();
	}

	/**
	 * @see javax.servlet.http.HttpServletRequest#getRequestURI()
	 */
	public String getRequestURI() {
		return this.request.getRequestURI();
	}

	/**
	 * @see javax.servlet.http.HttpServletRequest#getRequestURL()
	 */
	public StringBuffer getRequestURL() {
		return this.request.getRequestURL();
	}

	/**
	 *  Takes the modified servlet path as an argument. This path
	 *  should end in a "/"
	 *  @param servletPath String
	 */
	public void setServletPath(String servletPath){
		this.servletPath = servletPath;
	}

	/**
	 *  Returns a modified servlet path that addes path information from
	 *  OpenCms VFS as if it were on the real filesystem.
	 * 
	 */
	public String getServletPath() {
		if(this.servletPath == null){
			return this.request.getServletPath();
		}
		
		return this.servletPath;
		
	}

	/**
	 * @see javax.servlet.http.HttpServletRequest#getSession()
	 */
	public HttpSession getSession() {
		return this.request.getSession();
	}

	/**
	 * @see javax.servlet.http.HttpServletRequest#getSession(boolean)
	 */
	public HttpSession getSession(boolean arg0) {
		return this.request.getSession(arg0);
	}

	/**
	 * @see javax.servlet.http.HttpServletRequest#getUserPrincipal()
	 */
	public Principal getUserPrincipal() {
		return this.request.getUserPrincipal();
	}

	/**
	 * @see javax.servlet.http.HttpServletRequest#isRequestedSessionIdFromCookie()
	 */
	public boolean isRequestedSessionIdFromCookie() {
		return this.request.isRequestedSessionIdFromCookie();
	}

	/**
	 * @see javax.servlet.http.HttpServletRequest#isRequestedSessionIdFromUrl()
	 * @deprecated
	 */
	public boolean isRequestedSessionIdFromUrl() {
		return this.request.isRequestedSessionIdFromUrl();
	}

	/**
	 * @see javax.servlet.http.HttpServletRequest#isRequestedSessionIdFromURL()
	 */
	public boolean isRequestedSessionIdFromURL() {
		return this.request.isRequestedSessionIdFromURL();
	}

	/**
	 * @see javax.servlet.http.HttpServletRequest#isRequestedSessionIdValid()
	 */
	public boolean isRequestedSessionIdValid() {
		return this.request.isRequestedSessionIdValid();
	}

	/**
	 * @see javax.servlet.http.HttpServletRequest#isUserInRole(String)
	 */
	public boolean isUserInRole(String arg0) {
		return this.request.isUserInRole(arg0);
	}

	/**
	 * @see javax.servlet.ServletRequest#getAttribute(String)
	 */
	public Object getAttribute(String arg0) {
		return this.request.getAttribute(arg0);
	}

	/**
	 * @see javax.servlet.ServletRequest#getAttributeNames()
	 */
	public Enumeration getAttributeNames() {
		return this.request.getAttributeNames();
	}

	/**
	 * @see javax.servlet.ServletRequest#getCharacterEncoding()
	 */
	public String getCharacterEncoding() {
		return this.request.getCharacterEncoding();
	}

	/**
	 * @see javax.servlet.ServletRequest#getContentLength()
	 */
	public int getContentLength() {
		return this.request.getContentLength();
	}

	/**
	 * @see javax.servlet.ServletRequest#getContentType()
	 */
	public String getContentType() {
		return this.request.getContentType();
	}

	/**
	 * @see javax.servlet.ServletRequest#getInputStream()
	 */
	public ServletInputStream getInputStream() throws IOException {
		return this.request.getInputStream();
	}

	/**
	 * @see javax.servlet.ServletRequest#getLocale()
	 */
	public Locale getLocale() {
		return this.request.getLocale();
	}

	/**
	 * @see javax.servlet.ServletRequest#getLocales()
	 */
	public Enumeration getLocales() {
		return this.request.getLocales();
	}

	/**
	 * @see javax.servlet.ServletRequest#getParameter(String)
	 */
	public String getParameter(String arg0) {
		return this.request.getParameter(arg0);
	}

	/**
	 * @see javax.servlet.ServletRequest#getParameterMap()
	 */
	public Map getParameterMap() {
		return this.request.getParameterMap();
	}

	/**
	 * @see javax.servlet.ServletRequest#getParameterNames()
	 */
	public Enumeration getParameterNames() {
		return this.request.getParameterNames();
	}

	/**
	 * @see javax.servlet.ServletRequest#getParameterValues(String)
	 */
	public String[] getParameterValues(String arg0) {
		return this.request.getParameterValues(arg0);
	}

	/**
	 * @see javax.servlet.ServletRequest#getProtocol()
	 */
	public String getProtocol() {
		return this.request.getProtocol();
	}

	/**
	 * @see javax.servlet.ServletRequest#getReader()
	 */
	public BufferedReader getReader() throws IOException {
		return this.request.getReader();
	}

	/**
	 * @see javax.servlet.ServletRequest#getRealPath(String)
	 * @deprecated
	 */
	public String getRealPath(String arg0) {
		return this.request.getRealPath(arg0);
	}

	/**
	 * @see javax.servlet.ServletRequest#getRemoteAddr()
	 */
	public String getRemoteAddr() {
		return this.request.getRemoteAddr();
	}

	/**
	 * @see javax.servlet.ServletRequest#getRemoteHost()
	 */
	public String getRemoteHost() {
		return this.request.getRemoteHost();
	}

	/**
	 * @see javax.servlet.ServletRequest#getRequestDispatcher(String)
	 */
	public RequestDispatcher getRequestDispatcher(String arg0) {
		return this.request.getRequestDispatcher(arg0);
	}

	/**
	 * @see javax.servlet.ServletRequest#getScheme()
	 */
	public String getScheme() {
		return this.request.getScheme();
	}

	/**
	 * @see javax.servlet.ServletRequest#getServerName()
	 */
	public String getServerName() {
		return this.request.getServerName();
	}

	/**
	 * @see javax.servlet.ServletRequest#getServerPort()
	 */
	public int getServerPort() {
		return this.request.getServerPort();
	}

	/**
	 * @see javax.servlet.ServletRequest#isSecure()
	 */
	public boolean isSecure() {
		return this.request.isSecure();
	}

	/**
	 * @see javax.servlet.ServletRequest#removeAttribute(String)
	 */
	public void removeAttribute(String arg0) {
		this.request.removeAttribute(arg0);
	}

	/**
	 * @see javax.servlet.ServletRequest#setAttribute(String, Object)
	 */
	public void setAttribute(String arg0, Object arg1) {
		this.request.setAttribute(arg0, arg1);
	}

	/**
	 * @see javax.servlet.ServletRequest#setCharacterEncoding(String)
	 */
	public void setCharacterEncoding(String arg0)
		throws UnsupportedEncodingException {
			this.request.setCharacterEncoding(arg0);
	}

	public String getLocalAddr() {
		// TODO Auto-generated method stub
		return null;
	}

	public String getLocalName() {
		// TODO Auto-generated method stub
		return null;
	}

	public int getLocalPort() {
		// TODO Auto-generated method stub
		return 0;
	}

	public int getRemotePort() {
		// TODO Auto-generated method stub
		return 0;
	}

}
